package com.hbnu.system.utils;

import cn.hutool.core.util.StrUtil;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;

public class DateUtil {

    public static LocalDateTime dateToLocalDateTime(String str)
    {
        String[] time = str.split("-");
        Calendar calendar = Calendar.getInstance();

        if (time.length ==3 )
        {
            calendar.set(Calendar.YEAR,Integer.valueOf(time[0]));
            calendar.set(Calendar.MONDAY,Integer.valueOf(time[1])-1);
            calendar.set(Calendar.DATE,Integer.valueOf(time[2]));
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
        }
        else if (time.length == 2)
        {
            calendar.set(Calendar.YEAR,Integer.valueOf(time[0]));
            calendar.set(Calendar.MONDAY,Integer.valueOf(time[1])-1);
            calendar.set(Calendar.DAY_OF_MONTH,1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
        }
        else if (time.length == 1 && StrUtil.isNotBlank(time[0]))
        {
            calendar.set(Calendar.YEAR,Integer.valueOf(time[0]));
            calendar.set(Calendar.MONDAY,0);
            calendar.set(Calendar.DAY_OF_MONTH,1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
        }
        else
        {
            return null;
        }
//        System.out.println("************************" + calendar.getTime());
        return calendar.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
}
